#include <commandReadBaseInform.h>
#include <commandList.h>
#include <deviceOperateSN.h>

#if COMMAND_MASTER_CALL_DEFINE
int masterCallbackReadBaseInfo(void *inputstring, uint32_t *inputsize, void *userdata)
{
    struct deviceStructReadBaseInfo *tmpinform = (struct deviceStructReadBaseInfo *)inputstring;
    struct protocolPackageCommonInform *tmppack = (struct protocolPackageCommonInform *)PV3_GET_COMM_BY_DATA(inputstring);
    // struct commandCommonStruct *tmpcommand = (struct commandCommonStruct *)PV3_GET_CMD_BY_DATA(inputstring);
    // struct protocolProcessorInform *tmpprocessor = (struct protocolProcessorInform *)userdata;
    if (!inputstring || !inputsize || !userdata)
    {
        logerror("");
        return -1;
    }
    logdebug("device[%s]'s version: %s, sn: %s, type=%d", getMaxSizeHexStr(tmppack->v3.v3DesAddr).byteStr, tmpinform->version,
             getMaxSizeHexStr(tmpinform->serialnumber).byteStr, tmpinform->deviceType);
    return 0;
}
int masterWaitResponseReadBaseInfo(struct protocolProcessorInform *processer, struct masterStructReadBaseInfo *indata, uint16_t insize, uint8_t *devaddr)
{
    int tmpflag = 0;
    if (!processer)
    {
        logerror("");
        return -1;
    }
    uint8_t tmpPackBuffer[PROTOCOL_PACK_MAX_SIZE];
    struct protocolPackageCommonInform *tmppack = (struct protocolPackageCommonInform *)tmpPackBuffer;
    struct commandCommonStruct *tmpcommand = (struct commandCommonStruct *)tmppack->v3.v3Data;
    if ((PROTOCOL_V3_COMMON_HEAD_SIZE + PROTOCOL_V3_DATA_SIZE(insize)) > sizeof(tmpPackBuffer))
    {
        logerror("data big:%d", (int)PROTOCOL_V3_DATA_SIZE(insize));
        return 0;
    }
    memcpy(processer->commandExcuterAddr, devaddr, sizeof(processer->commandExcuterAddr));
    processer->commandExcuter.commandNB = commandIdReadBaseInfo;
    processer->commandExcuter.commandSN = generateCommandSerial();
    processer->commandExcuter.responseFlag = commandExcuteResultUnkown;
    memcpy(tmpcommand, &processer->commandExcuter, PROTOCOL_COMMAND_COMMON_HEAD_SIZE);
    memcpy(tmpcommand->commandData, indata, insize);
    tmppack->v3.v3DataSize = PROTOCOL_V3_DATA_SIZE(insize);
    tmpflag = processerPackageProtocolData(processer, tmpPackBuffer, sizeof(tmpPackBuffer));
    if (tmpflag <= 0)
    {
        logerror("");
        return -1;
    }
    masterProcesserSetReTryCount(processer, 2);
    masterProcesserSetTimeout(processer, 400);
    tmpflag = masterProcesserSendAndWaitResponse(processer, tmpPackBuffer, tmpflag);
    return tmpflag;
}
#endif

#if COMMAND_DEVICE_CALL_DEFINE
struct deviceParameterOperate *devParamNormalOpt();
int deviceCallbackReadBaseInfo(void *inputstring, uint32_t *inputsize, void *userdata)
{
    struct masterStructReadBaseInfo *tmpCmdData = (struct masterStructReadBaseInfo *)inputstring;
    struct deviceStructReadBaseInfo *tmpResponseData = (struct deviceStructReadBaseInfo *)inputstring;
    // struct protocolPackageCommonInform *tmppackage = (struct protocolPackageCommonInform *)inputstring;
    // struct commandCommonStruct *tmpcommand = (struct commandCommonStruct *)tmppackage->v3.v3Data;
    // struct protocolProcessorInform *tmpprocessor = (struct protocolProcessorInform *)userdata;
    if (!inputstring || !inputsize || !userdata)
    {
        logerror("");
        return -1;
    }
    // if (inputsize[0] != sizeof(struct masterStructReadBaseInfo))
    // {
    //     logerror("input data error :%d, %d", inputsize[0], (int)sizeof(struct masterStructReadBaseInfo));
    //     return -1;
    // }
    if (sizeof(struct deviceStructReadBaseInfo) > PROTOCOL_V3_COMMAND_DATA_SIZE)
    {
        logerror("size error:%d>%d", (int)sizeof(struct deviceStructReadBaseInfo), (int)PROTOCOL_V3_COMMAND_DATA_SIZE);
        return -1;
    }

    logdebug("ver:%s,sn:%s,type:%d", DEVICE_VERSION_STR_DATA, getMaxSizeHexStr(deviceSnString).byteStr,
             DEVICE_TYPE_OF_THIS);

    inputsize[0] = sizeof(struct deviceStructReadBaseInfo);
    memcpypro(tmpResponseData->version, sizeof(tmpResponseData->version), DEVICE_VERSION_STR_DATA, sizeof(DEVICE_VERSION_STR_DATA));
    memcpypro(tmpResponseData->serialnumber, sizeof(tmpResponseData->serialnumber), deviceSnString, sizeof(deviceSnString));
    tmpResponseData->deviceType = DEVICE_TYPE_OF_THIS;

    return commandExcuteResultOk;
}
#endif